package com.bindapp.widget;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * Created by apple on 2018/7/1.
 */

public class GridDecoration extends RecyclerView.ItemDecoration {
    private Context context;
    private int decoration,count;
    private boolean ishead=false;
    public GridDecoration(Context context,int decoration,int count) {
        this.decoration = decoration;
        this.count=count;
        this.context=context;
    }
    public GridDecoration(Context context,int decoration,int count,boolean ishead) {
        this.decoration = decoration;
        this.count=count;
        this.context=context;
        this.ishead=ishead;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.top = convertDpToPixel(7);
        outRect.bottom = 0;

        int itemposition = parent.getChildLayoutPosition(view) % count;//每行3个


        if(ishead){
            itemposition = itemposition-1;


        }
        if(itemposition==0&&count==1){
            outRect.left = convertDpToPixel(decoration*2);
            outRect.right = convertDpToPixel(decoration*2);
        }else if(itemposition==0&&count!=1){
            outRect.left = convertDpToPixel(decoration*2);
            outRect.right = convertDpToPixel(0);
        }else if(itemposition==(count-1)&&count!=-1){
            outRect.left = convertDpToPixel(decoration);
            outRect.right = convertDpToPixel(decoration*2);
        }else{
            outRect.left = convertDpToPixel(decoration);
            outRect.right = convertDpToPixel(0);
        }

        //for(int i=0;i<count;i++){



    }

    private int convertDpToPixel(int dp) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return (int) (dp * displayMetrics.density);
    }
}
